package com.ivan.hospital.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ivan.hospital.mapper.DepartmentMapper;
import com.ivan.model.entity.hosp.Department;
import com.ivan.hospital.service.DepartmentService;
import com.ivan.model.vo.hosp.DepartmentQueryVo;
import com.ivan.model.vo.hosp.DepartmentVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * @Author      Ivan
 * @Date        2023/4/4 20:39
 * @Description
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    /**
     * 条件查询医院科室带分页
     * @param current
     * @param limit
     * @param queryVo
     * @return
     */
    @Override
    public Page<Department> findByPage(long current, long limit, DepartmentQueryVo queryVo) {
        //分页对象
        Page<Department> page = new Page<>(current, limit);
        //查询条件
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        String depcode = queryVo.getDepcode();//科室编号
        String depname = queryVo.getDepname();//科室名称
        if (!StringUtils.isEmpty(depcode)) {
            wrapper.eq("depcode", queryVo.getDepcode());
        }
        if (!StringUtils.isEmpty(depname)) {
            wrapper.like("depname", queryVo.getDepname());
        }
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 根据id查询医院科室
     * @param id
     * @return
     */
    @Override
    public Department findById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据id删除医院科室
     * @param id
     * @return
     */
    @Override
    public int removeById(Long id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 根据idList批量删除医院科室
     * @param idList
     * @return
     */
    @Override
    public int removeByIds(List<Long> idList) {
        return baseMapper.deleteBatchIds(idList);
    }

    /**
     * 修改医院科室
     * @param department
     * @return
     */
    @Override
    public int updateDepartment(Department department) {
        return baseMapper.updateById(department);
    }

    /**
     * 添加医院科室
     * @param department
     * @return
     */
    @Override
    public int saveDepartment(Department department) {
        return baseMapper.insert(department);
    }

    /**
     * 根据hascode查询所有科室
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> getDeptListByHascode(String hoscode) {
        List<DepartmentVo> results = new ArrayList<>();
        //查询所有科室
        List<Department> departments = baseMapper.selectList(null);
        Map<String, List<Department>> listMap = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (String key : listMap.keySet()) {
            //所有的小科室
            List<Department> smallDepts = listMap.get(key);
            //封装大科室
            DepartmentVo bigVo = new DepartmentVo();
            bigVo.setDepcode(key);
            bigVo.setDepname(smallDepts.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            smallDepts.forEach(d -> {
                DepartmentVo smallVo = new DepartmentVo();
                smallVo.setDepcode(d.getDepcode());
                smallVo.setDepname(d.getDepname());
                children.add(smallVo);
            });
            bigVo.setChildren(children);
            //保存一个大科室
            results.add(bigVo);
        }
        return results;
    }
}
